<?php
namespace Addons\Wechat\Controller;
class SettingController extends \Addon {

	public function __construct() {
		parent::__construct();
		//dump($this->admin_store_info);
	}

	/*******微信菜单设置*****/
	/**
	 * [wechatMenu 微信菜单页]
	 * tree() 格式化
	 * @return [type] [description]
	 */
	public function wechatMenu() {
		$store_info = $this->admin_store_info;
		$this->admin_store_id = $store_info['store_id'];
		//dump($this->admin_store_id);exit();
		$this->memuNav = "wechatMenu";
		$data = BoxModel("addon_wx_menu")->where("store_id='$this->admin_store_id'")->order("sort_order")->select();
		$this->data = getTree($data);
		$this->admin_display();
	}

	/**
	 * [wmenuAdd 添加 修改微信菜单页]
	 * @return [type] [description]
	 */
	public function wmenuAdd() {
		$this->memuNav = "wechatMenu";
		$this->id = I('id');
		if (!empty($this->id)) {
			$this->list = BoxModel("addon_wx_menu")->load($this->id);
		}
		$data = BoxModel("addon_wx_menu")->where("store_id='$this->admin_store_id'")->order("sort_order")->select();
		$this->data = getTree($data);
		$this->admin_display();
	}

	/**********发布微信菜单接口********/
	public function wmenuCreate() {

		$wechatconfig = BoxModel('addon_wx_config')->where("store_id='$this->admin_store_id'")->field("appID,appsecret")->find();
		if ($wechatconfig['appID'] && $wechatconfig['appsecret']) {
			$memudata = BoxModel("addon_wx_menu")->where("store_id='$this->admin_store_id' and pid=0 and status=0")->field("id,type,name,url")->order("sort_order")->limit(3)->select();
			// if(count($memudata)!=3)$this->error('不够3个一级菜单！！！！');
			/******信菜单类型****/
			foreach ($memudata as $key => $one) {
				if ($one['type'] == 'click') {
					$memudata[$key]['key'] = $one['url'];
					unset($memudata[$key]['url']);
				}
			}
			$wecatMemu = array();
			/******************生成信菜单配置***************/
			foreach ($memudata as $key => $one) {
				$sub_button = array();
				$pid = $one['id'];
				$sub_button = BoxModel("addon_wx_menu")->where("pid='$pid' and status=0")->order("sort_order")->field("type,name,url")->limit(5)->select();
				/******信菜单类型****/
				foreach ($sub_button as $k => $one1) {
					if ($one1['type'] == 'click') {
						$sub_button[$k]['key'] = $one1['url'];
						unset($sub_button[$k]['url']);
					}
				}
				if ($sub_button) {
					$wecatMemu[$key]['name'] = $one['name'];
					$wecatMemu[$key]['sub_button'] = $sub_button;
				} else {
					unset($one['id']);
					$wecatMemu[$key] = $one;
				}
			}

			/********************推送到微信**************************/
			$weiXin = new \Common\Org\weiXin($wechatconfig);
			//dump($weiXin);exit();
			$r = $weiXin->createMemu($wecatMemu);
			$data = json_decode($r, true);
			if ($data['errmsg'] == "ok") {
				$this->jump('配置成功！');
			} else {
				$this->jump('生成失败！，错误信息：' . $data['errcode'] . ":" . $data['errmsg']);
			}

		} else {
			$this->jump('请先设置微信，appID、appsecret！');
		}
	}

	/**
	 * [wmenuInsert 添加 修改微信菜单方法]
	 * @return [type] [description]
	 */
	public function wmenuInsert() {
		$this->id = I('id');
		if (IS_POST) {
			if (empty($this->id)) {
				$data = array(
					'pid' => I('post.pid', '', 'intval'),
					'name' => I('post.name'),
					'url' => stripslashes(htmlspecialchars_decode($_POST['url'])),
					'type' => I('post.type'),
					'created' => time(),
					'store_id' => $this->admin_store_id,
				);
				if (BoxModel("addon_wx_menu")->add($data)) {
					$this->redirect(addons_url('wechatMenu'));
				} else {
					$this->error('添加微信菜单失败！');
				}
			} else {
				$arr = array(
					'name' => I('post.name'),
					'url' => stripslashes(htmlspecialchars_decode($_POST['url'])),
					'type' => I('post.type'),
					'status' => I('status'),
				);
				BoxModel("addon_wx_menu")->where("id='$this->id'")->save($arr);
				$this->redirect(addons_url('wechatMenu'));
			}
		}
	}

	/**
	 * [upMsort 更新微信菜单排序]
	 * @return [type] [description]
	 */
	public function upMsort() {
		foreach ($_POST as $id => $sort) {
			BoxModel('addon_wx_menu')->where(array('id' => $id))->setField('sort', $sort);
		}
		$this->redirect(addons_url('wechatMenu'));
	}

	/**
	 * [wmenuDel 删除微信菜单]
	 * @return [type] [description]
	 */
	public function wmenuDel() {
		$this->id = I('id');
		$count = BoxModel('addon_wx_menu')->where(array("pid" => $this->id))->count();
		if ($count > 0) {
			$this->jump("该菜单下还有子菜单，无法删除！");
		}
		if (BoxModel("addon_wx_menu")->delete($this->id)) {
			$this->redirect(addons_url('wechatMenu'));
		} else {
			$this->jump('删除失败！');
		}
	}

	/**
	 * [add 添加或修改微信帐号页]
	 */
	public function config() {
		$store_info = $this->admin_store_info;
		$this->admin_store_id = $store_info['store_id'];
		$this->store_info = $store_info;
		//dump($this->admin_store_id);
		if (!empty($this->admin_store_id)) {
			$this->data = BoxModel("addon_wx_config")->where(array('store_id' => $this->admin_store_id))->find();
		}
		$this->admin_display();
	}

	/**
	 * [checkwechat 检测微信号是否重复]
	 * @return [type] [description]
	 */
	public function checkwechat() {
		$store_info = $this->admin_store_info;
		$this->admin_store_id = $store_info['store_id'];
		$store_wechat = I('store_wechat');
		$check = BoxModel("addon_wx_config")->where("store_wechat='$store_wechat' and store_id<>'$this->admin_store_id'")->find();
		if ($check) {
			echo 'false';
		} else {
			echo 'true';
		}
	}

	/**
	 * [wechatInsert 添加或修改微信帐号方法]
	 *
	 */
	public function wechatInsert() {
		$model = BoxModel("addon_wx_config");
		$store_info = $this->admin_store_info;
		$this->admin_store_id = $store_info['store_id'];
		$check = BoxModel("addon_wx_config")->where(array('store_id' => $this->admin_store_id))->find();
		if (IS_POST) {
			$arr = $model->create();
			if ($check) {
				$model->where(array('store_id' => $this->admin_store_id))->save($arr);
			} else {
				$arr['store_id'] = $this->admin_store_id;
				$model->add($arr);
			}
			$this->jump("操作成功");

		}
	}

}